﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;
using HRM.DAL.Report;

namespace HRM.UI.HRManagerUI.EmpModule
{
    public partial class ChangeStatus : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            statusMsg.Visible = false;
            lblStatusMsg.Visible = false;
            EmpinfoTb.Visible = false;
            changestatusTb.Visible = false;
        }
        protected void FindEmployee(object o, EventArgs e)
        {
           
            PopulateEmployeeInfo();
        }
        private void PopulateEmployeeInfo()
        {
            try
            {
                //Employee General Info
                Code code = new Code();
                code.CodeName = txtEmpCode.Value;
                ViewState["code"] = txtEmpCode.Value;
                ViewEmployee viewEmployee = new ViewEmployee();
                EmployeeManager manager = new EmployeeManager();
                viewEmployee = manager.SearchEmployeeByCode(code);
                lblEmpStatus.Text = viewEmployee.Employee.Status;
                lblempName.Text = viewEmployee.Employee.Name;
                lblDesignation.Text = viewEmployee.Designation.Title;
                lblProject.Text = viewEmployee.Project.ProjectName;
                lblJoiningDate.Text = viewEmployee.Employee.JoiningDate.ToShortDateString();
                lblBirth.Text = viewEmployee.Employee.DOB.ToShortDateString();
                empImage.ImageUrl = viewEmployee.Photo.FileName;
                EmpinfoTb.Visible = true;
                changestatusTb.Visible = true;

            }
            catch (Exception ex)
            {
                lblStatusMsg.Visible = true;
                lblStatusMsg.Text = "Employee not found by this code. Please try again";
                lblStatusMsg.ForeColor = Color.Red;
                ClearForm();
                EmpinfoTb.Visible = false;
                changestatusTb.Visible = false;
            }

        }

        private void ClearForm()
        {
            lblEmpStatus.Text = "";
            lblempName.Text = "";
            lblDesignation.Text = "";
            lblProject.Text = "";
            lblJoiningDate.Text = "";
            lblBirth.Text = "";
            empImage.ImageUrl = "";
            empImage.AlternateText = "Photo";
        }

        protected void ChangeEmpStatus(object o, EventArgs e)
        {
            try
            {
                Code code = new Code();
                code.CodeName = ViewState["code"].ToString();
                string status = ddlStatus.Value;
                EmployeeManager manager = new EmployeeManager();
                if (manager.ChangeStatus(code, status))
                {
                    statusMsg.Visible = true;
                    statusMsg.ForeColor = Color.Green;
                    statusMsg.Text = "Successfully Changed Status for the Employee";
                    changestatusTb.Visible = true;
                    EmpinfoTb.Visible = false;
                    
                }
            }
            catch (Exception ex)
            {
                statusMsg.Visible = true;
                statusMsg.ForeColor = Color.OrangeRed;
                statusMsg.Text = ex.Message;
            }
        }
    }
}